#include "json_exmple.h"



static const char *TAG = "cJSON";


void simple_json_parse1(void)
{
    ESP_LOGI(TAG, "\r\nsimple_json_parse1------------------------------------------------------------");
    char *str = 
    "{                                          \
        \"Id\":\"cJSON application by yuezheng.gao\",      \
        \"Code\":200,                           \
        \"Success\":true                        \
    }";
    cJSON* root = NULL;
    root = cJSON_Parse(str);
    if(root==NULL)
    {
        ESP_LOGE(TAG, "simple_json_parse1 cJSON_Parse error!");
        return;
    }
    ESP_LOGI(TAG, "Id: %s", cJSON_GetStringValue(cJSON_GetObjectItem(root, "Id")));
    ESP_LOGI(TAG, "Code: %d", cJSON_GetObjectItem(root, "Code")->valueint);
    ESP_LOGI(TAG, "Success: %d", cJSON_GetObjectItem(root, "Success")->valueint);
    cJSON_Delete(root);
}

void simple_json_create1(void)
{
    ESP_LOGI(TAG, "\r\nsimple_json_create1------------------------------------------------------------");
    cJSON* root = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "Id", "cJSON application by yuezheng.gao");
    cJSON_AddNumberToObject(root, "Code", 200);
    cJSON_AddBoolToObject(root, "Success", true);
    ESP_LOGI(TAG, "\r\n %s", cJSON_Print(root));
    cJSON_Delete(root);
}

void simple_json_parse2(void)
{
    ESP_LOGI(TAG, "\r\nsimple_json_parse2------------------------------------------------------------");
    char *str = 
    "{                                          \
        \"Id\":\"cJSON application by yuezheng.gao\",      \
        \"Code\":200,                           \
        \"Success\":true,                        \
        \"Data\":{                              \
            \"Status\":\"ONLINE\",               \
            \"Timestamp\":1650987758240         \
        }                                       \
    }";
    cJSON* root = NULL;
    cJSON* data = NULL;
    root = cJSON_Parse(str);
    if(root==NULL)
    {
        ESP_LOGE(TAG, "simple_json_parse2 cJSON_Parse error!");
        return;
    }
    ESP_LOGI(TAG, "Id: %s", cJSON_GetStringValue(cJSON_GetObjectItem(root, "Id")));
    ESP_LOGI(TAG, "Code: %d", cJSON_GetObjectItem(root, "Code")->valueint);
    ESP_LOGI(TAG, "Success: %d", cJSON_GetObjectItem(root, "Success")->valueint);

    data = cJSON_GetObjectItem(root, "Data");
    ESP_LOGI(TAG, "Data.Status: %s", cJSON_GetStringValue(cJSON_GetObjectItem(data, "Status")));
    ESP_LOGI(TAG, "Data.Timestamp: %d", cJSON_GetObjectItem(data, "Timestamp")->valueint);

    cJSON_Delete(root);
}

void simple_json_create2(void)
{
    ESP_LOGI(TAG, "\r\nsimple_json_create2------------------------------------------------------------");
    cJSON* root = cJSON_CreateObject();
    cJSON* data = cJSON_CreateObject();
    cJSON_AddStringToObject(root, "Id", "cJSON application by yuezheng.gao");
    cJSON_AddNumberToObject(root, "Code", 200);
    cJSON_AddBoolToObject(root, "Success", true);
    // cJSON_AddTrueToObject(root, "Success");
    
    cJSON_AddStringToObject(data, "Status", "ONLINE");
    cJSON_AddNumberToObject(data, "Timestamp", 1650987758240);
    cJSON_AddItemToObject(root, "Data", data);
    ESP_LOGI(TAG, "\r\n %s", cJSON_Print(root));

    cJSON_Delete(root);
}

void complex_json_parse1(void)
{
    ESP_LOGI(TAG, "\r\ncomplex_json_parse1------------------------------------------------------------");
    char *str = 
    "{                                          \
        \"Id\":\"cJSON application by yuezheng.gao\",      \
        \"Code\":200,                           \
        \"Success\":true,                        \
        \"DataArray\":[                         \
            0,                                  \
            1,                                  \
            2,                                  \
            3,                                  \
            4,                                  \
            5,                                  \
            6,                                  \
            7,                                  \
            8,                                  \
            9                                  \
        ]                                       \
    }";
    cJSON* root = NULL;
    cJSON* data_array = NULL;
    root = cJSON_Parse(str);
    if(root==NULL)
    {
        ESP_LOGE(TAG, "complex_json_parse1 cJSON_Parse root error!");
        return;
    }
    ESP_LOGI(TAG, "Id: %s", cJSON_GetStringValue(cJSON_GetObjectItem(root, "Id")));
    ESP_LOGI(TAG, "Code: %d", cJSON_GetObjectItem(root, "Code")->valueint);
    ESP_LOGI(TAG, "Success: %d", cJSON_GetObjectItem(root, "Success")->valueint);

    data_array = cJSON_GetObjectItem(root, "DataArray");
    if(data_array==NULL)
    {
        ESP_LOGE(TAG, "complex_json_parse1 cJSON_Parse DataArray error!");
        return;
    }
    for(int index=0; index<cJSON_GetArraySize(data_array); index++)
    {
        ESP_LOGI(TAG, "DataArray[%d]: %d", index, cJSON_GetArrayItem(data_array, index)->valueint);
    }

    cJSON_Delete(root);
}

void complex_json_create1(void)
{
    ESP_LOGI(TAG, "\r\ncomplex_json_create1------------------------------------------------------------");
    cJSON* root = cJSON_CreateObject();
    cJSON* data_array = cJSON_CreateArray();
    cJSON_AddStringToObject(root, "Id", "cJSON application by yuezheng.gao");
    cJSON_AddNumberToObject(root, "Code", 200);
    cJSON_AddBoolToObject(root, "Success", true);
    
    for(int index=0; index<10; index++)
    {
        cJSON_AddItemToArray(data_array, cJSON_CreateNumber(index));
    }
    cJSON_AddItemToObject(root, "DataArray", data_array);
    ESP_LOGI(TAG, "\r\n %s", cJSON_Print(root));

    cJSON_Delete(root);
}

void complex_json_parse2(void)
{
    ESP_LOGI(TAG, "\r\ncomplex_json_parse2------------------------------------------------------------");
    char *str = 
    "{                                          \
        \"Id\":\"cJSON application by yuezheng.gao\",      \
        \"Code\":200,                           \
        \"Success\":true,                        \
        \"DataArray\":[                         \
            {                                   \
                \"Status\":\"ONLINE\",          \
                \"Timestamp\":1650987758240     \
            },                                  \
            {                                   \
                \"Status\":\"OFFLINE\",         \
                \"Timestamp\":1650987750000     \
            }                                   \
        ]                                       \
    }";
    cJSON* root = NULL;
    cJSON* data_array = NULL;
    root = cJSON_Parse(str);
    if(root==NULL)
    {
        ESP_LOGE(TAG, "complex_json_parse2 cJSON_Parse root error!");
        return;
    }
    ESP_LOGI(TAG, "Id: %s", cJSON_GetStringValue(cJSON_GetObjectItem(root, "Id")));
    ESP_LOGI(TAG, "Code: %d", cJSON_GetObjectItem(root, "Code")->valueint);
    ESP_LOGI(TAG, "Success: %d", cJSON_GetObjectItem(root, "Success")->valueint);

    data_array = cJSON_GetObjectItem(root, "DataArray");
    if(data_array==NULL)
    {
        ESP_LOGE(TAG, "complex_json_parse2 cJSON_Parse DataArray error!");
        return;
    }
    for(int index=0; index<cJSON_GetArraySize(data_array); index++)
    {
        ESP_LOGI(TAG, "DataArray[%d].Status: %s", index, cJSON_GetObjectItem(cJSON_GetArrayItem(data_array, index), "Status")->valuestring);
        ESP_LOGI(TAG, "DataArray[%d].Timestamp: %f", index, cJSON_GetObjectItem(cJSON_GetArrayItem(data_array, index), "Timestamp")->valuedouble);
    }

    cJSON_Delete(root);
}

void complex_json_create2(void)
{
    ESP_LOGI(TAG, "\r\ncomplex_json_create2------------------------------------------------------------");
    cJSON* root = cJSON_CreateObject();
    cJSON* data_array = cJSON_CreateArray();
    cJSON_AddStringToObject(root, "Id", "cJSON application by yuezheng.gao");
    cJSON_AddNumberToObject(root, "Code", 200);
    cJSON_AddBoolToObject(root, "Success", true);
    
    for(int index=0; index<2; index++)
    {
        cJSON* data = cJSON_CreateObject();
        cJSON_AddStringToObject(data, "Status", "ONLINE");
        cJSON_AddNumberToObject(data, "Timestamp", 1650987758240);
        cJSON_AddItemToArray(data_array, data);
    }
    cJSON_AddItemToObject(root, "DataArray", data_array);
    ESP_LOGI(TAG, "\r\n %s", cJSON_Print(root));

    cJSON_Delete(root);
}

void complex_json_parse3(void)
{
    ESP_LOGI(TAG, "\r\ncomplex_json_parse3------------------------------------------------------------");
    char *str = 
    "{                                          \
        \"Id\":\"cJSON application by yuezheng.gao\",      \
        \"Code\":200,                           \
        \"Success\":true,                        \
        \"Data\":{                              \
            \"PropertyStatusInfoArray\":[       \
                {                               \
                    \"Status\":\"ONLINE\",      \
                    \"Timestamp\":1650987758240 \
                },                              \
                {                               \
                    \"Status\":\"OFFLINE\",     \
                    \"Timestamp\":1650987750000 \
                }                               \
            ]                                   \
        }                                       \
    }";
    cJSON* root = NULL;
    cJSON* data = NULL;
    cJSON* property_status_info_array = NULL;
    root = cJSON_Parse(str);
    if(root==NULL)
    {
        ESP_LOGE(TAG, "complex_json_parse3 cJSON_Parse root error!");
        return;
    }
    ESP_LOGI(TAG, "Id: %s", cJSON_GetStringValue(cJSON_GetObjectItem(root, "Id")));
    ESP_LOGI(TAG, "Code: %d", cJSON_GetObjectItem(root, "Code")->valueint);
    ESP_LOGI(TAG, "Success: %d", cJSON_GetObjectItem(root, "Success")->valueint);

    data = cJSON_GetObjectItem(root, "Data");
    if(data==NULL)
    {
        ESP_LOGE(TAG, "complex_json_parse3 cJSON_Parse Data error!");
        return;
    }
    property_status_info_array = cJSON_GetObjectItem(data, "PropertyStatusInfoArray");
    if(data==NULL)
    {
        ESP_LOGE(TAG, "complex_json_parse3 cJSON_Parse PropertyStatusInfoArray error!");
        return;
    }
    for(int index=0; index<cJSON_GetArraySize(property_status_info_array); index++)
    {
        ESP_LOGI(TAG, "Data.PropertyStatusInfoArray[%d].Status: %s", index, cJSON_GetObjectItem(cJSON_GetArrayItem(property_status_info_array, index), "Status")->valuestring);
        ESP_LOGI(TAG, "Data.PropertyStatusInfoArray[%d].Timestamp: %d", index, cJSON_GetObjectItem(cJSON_GetArrayItem(property_status_info_array, index), "Timestamp")->valueint);
    }

    cJSON_Delete(root);
}

void complex_json_create3(void)
{
    ESP_LOGI(TAG, "\r\ncomplex_json_create3------------------------------------------------------------");
    cJSON* root = cJSON_CreateObject();
    cJSON* data = cJSON_CreateObject();
    cJSON* property_status_info_array = cJSON_CreateArray();
    cJSON_AddStringToObject(root, "Id", "cJSON application by yuezheng.gao");
    cJSON_AddNumberToObject(root, "Code", 200);
    cJSON_AddBoolToObject(root, "Success", true);
    
    for(int index=0; index<2; index++)
    {
        cJSON* temp = cJSON_CreateObject();
        cJSON_AddStringToObject(temp, "Status", "ONLINE");
        cJSON_AddNumberToObject(temp, "Timestamp", 1650987758240);
        cJSON_AddItemToArray(property_status_info_array, temp);
    }
    cJSON_AddItemToObject(data, "PropertyStatusInfoArray", property_status_info_array);
    cJSON_AddItemToObject(root, "Data", data);
    ESP_LOGI(TAG, "\r\n %s", cJSON_Print(root));

    cJSON_Delete(root);
}